package com.simpleaccount.Login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.simpleaccount.MainActivity;
import com.simpleaccount.R;
import com.simpleaccount.Toast.ToastUtils;
import com.simpleaccount.User.AppUser;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import es.dmoral.toasty.Toasty;

/**
 * Created by zhou on 2017/7/12.
 */

public class RegisterActivity extends Activity implements View.OnClickListener {

    @Bind(R.id.register_password_input2)
    EditText registerPasswordInput2;
    private EditText registerAccountInput;
    private EditText registerPasswordInput;
    private EditText registerSetNickname;
    private EditText registerEmail;
    private EditText registerPhoneNo;
    private Button registerSubmit;
    private Button registerCancel;
    private LinearLayout registerDiv;//1

    private String userName;
    private String password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        registerAccountInput = (EditText) findViewById(R.id.register_account_input);
        registerPasswordInput = (EditText) findViewById(R.id.register_password_input);
        registerSetNickname = (EditText) findViewById(R.id.register_set_nickname);
        registerEmail = (EditText) findViewById(R.id.register_email);
        registerEmail.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//限制输入类型
        registerPhoneNo = (EditText) findViewById(R.id.register_phoneNo);
        registerPhoneNo.setInputType(InputType.TYPE_CLASS_PHONE);
        registerSubmit = (Button) findViewById(R.id.register_submit);
        registerCancel = (Button) findViewById(R.id.register_cancel);
        registerDiv = (LinearLayout) findViewById(R.id.register_div);

        registerSubmit.setOnClickListener(this);
        registerCancel.setOnClickListener(this);
    }

    public void onClick(View v1) {//onclick
        switch (v1.getId()) {//switch
            case R.id.register_cancel:
                Intent intent1 = new Intent(this, MainActivity.class);
                startActivity(intent1);
                break;

            case R.id.register_submit:
                userName = registerAccountInput.getText().toString();
                password = registerPasswordInput.getText().toString();
                if (!registerPasswordInput2.getText().toString().equals(registerPasswordInput.getText().toString())) {//fei kong
                    Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (userName.isEmpty() || password.isEmpty()) {//fei kong
                    ToastUtils.toast(this,"用户名和密码不能为空","warning");
                    return;
                }//fei kong
                else {//else
                    AppUser myUser = new AppUser();
                    myUser.setUsername(userName);
                    myUser.setPassword(password);
                    myUser.setNickName(registerSetNickname.getText().toString());
                    myUser.setEmail(registerEmail.getText().toString());
                    myUser.setMobilePhoneNumber(registerPhoneNo.getText().toString());
                    if(registerPhoneNo.getText().length()<11){

                        ToastUtils.toast(RegisterActivity.this,"请输入11位手机号","warning");

                    }

                    myUser.signUp(new SaveListener<AppUser>() {//signup
                        @Override
                        public void done(AppUser s, BmobException e) {//done
                            if (e == null) {//if e==null
                                ToastUtils.toast(RegisterActivity.this,"注册成功","success");
                                Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                                startActivity(intent1);
                            }//if e==null
                            else {//else e==null
                                if(e.getErrorCode()==202){
                                    ToastUtils.toast(RegisterActivity.this,"用户名已存在","warning");
                                }else if(e.getErrorCode()==203){
                                    ToastUtils.toast(RegisterActivity.this,"邮箱已被注册","warning");
                                }else if(e.getErrorCode()==209){
                                    ToastUtils.toast(RegisterActivity.this,"手机号已被注册","warning");
                                }else if(e.getErrorCode()==9016||e.getErrorCode()==9010){
                                    ToastUtils.toast(RegisterActivity.this, "网络错误，请检查您的网络", "err");
                                }
//                                ToastUtils.toast(RegisterActivity.this,"注册失败+\n"+e.getMessage(),"err");

                            }//else e==null
                        }//done

                    });//signup

                }//else
                break;
            default:
                break;
        }//switch
    }//onclick
    @Override
    protected void onPause() {
        super.onPause();
        ToastUtils.cancel();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ToastUtils.cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ToastUtils.cancel();
    }
}//1